Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't expand macros in some suggestions #3366

Merged
merged 3 commits into from Oct 28, 2018

Conversation

phansch
Copy link
Member

@phansch phansch commented Oct 27, 2018

Fixes #1148
Fixes #1628
Fixes #2455
Fixes #3023
Fixes #3333
Fixes #3360

@@ -72,7 +72,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
let a = cx.tables.expr_ty(e);
let b = cx.tables.expr_ty(&args[0]);
if same_tys(cx, a, b) {
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
let sugg = if in_macro(args[0].span) {
snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we unconditionally call the source callsite methods? if so, can we move it into the snippet function so all lints benefit from it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I was thinking that this is probably becoming a recurring pattern. I will give it a try!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem occurs when span.source_callsite() is called on a format! argument. This happens in useless_format lint (and a few other lints):

https://github.com/rust-lang-nursery/rust-clippy/blob/457e7f12e9fc028eae182f23b279194e5344b676/clippy_lints/src/format.rs#L78-L79

Unconditionally calling source_callsite will result in the wrong suggestion because the source_callsite exists and is the format! macro in this case:

help: consider using .to_string(): `format!("foo");.to_string()`

I think it still makes sense to have a separate snippet_with_macro_callsite function for the other cases, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. This generally only works if the entire situation is outside a macro, but most of our lints are skipping those situations via in_macro on the full span anyway.

@phansch phansch changed the title Don't expand macro in identity_conversion suggestion Don't expand some macros in suggestions Oct 27, 2018
@phansch phansch changed the title Don't expand some macros in suggestions WIP: Don't expand some macros in suggestions Oct 27, 2018
@phansch phansch changed the title WIP: Don't expand some macros in suggestions Don't expand macros in some suggestions Oct 27, 2018
@phansch
Copy link
Member Author

phansch commented Oct 27, 2018

I added a separate function called snippet_with_macro_callsite that can be used when it's clear that the input span is not a macro argument. I'm.. not sure if there is a better way. At least this way there are fewer conditionals around.

I will also go through all the other issues and try to find more cases that can be fixed easily.

edit: Ok, I can't find any more issues that will be fixed by this.

@phansch phansch added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 27, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Oct 27, 2018

bors r+

bors bot added a commit that referenced this pull request Oct 27, 2018
3366: Don't expand macros in some suggestions r=oli-obk a=phansch

Fixes #1148 
Fixes #1628
Fixes #2455
Fixes #3023
Fixes #3333
Fixes #3360

Co-authored-by: Philipp Hansch <dev@phansch.net>
@bors
Copy link
Contributor

bors bot commented Oct 27, 2018

Build failed

@phansch
Copy link
Member Author

phansch commented Oct 28, 2018

bors r=oli-obk

bors bot added a commit that referenced this pull request Oct 28, 2018
3217: Fix string_lit_as_bytes lint for macros r=phansch a=yaahallo

Prior to this change, string_lit_as_bytes would trigger for constructs
like `include_str!("filename").as_bytes()` and would recommend fixing it
by rewriting as `binclude_str!("filename")`.

This change updates the lint to act as an EarlyLintPass lint. It then
differentiates between string literals and macros that have bytes
yielding alternatives.

Closes #3205

3366: Don't expand macros in some suggestions r=oli-obk a=phansch

Fixes #1148 
Fixes #1628
Fixes #2455
Fixes #3023
Fixes #3333
Fixes #3360

Co-authored-by: Jane Lusby <jlusby42@gmail.com>
Co-authored-by: Philipp Hansch <dev@phansch.net>
@bors
Copy link
Contributor

bors bot commented Oct 28, 2018

@bors bors bot merged commit 840e50e into rust-lang:master Oct 28, 2018
@phansch phansch deleted the behebt_problem_3360 branch October 28, 2018 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants